#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/fsl_devices.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/workqueue.h>
#include <linux/sizes.h>

#include <linux/console.h>
#include <linux/types.h>
#include "button_drv.h"

static ssize_t button_read(struct file *file, char __user *buf, size_t size, loff_t *offset);
static int button_open(struct inode *node, struct file *flie);

static int major = 0;
static struct button_operation* p_button_opr;
static struct file_operations button_drv = {
    .open = button_open,
    .read = button_read,
};

static struct class * button_class;

void button_operation_register(struct button_operation* opr)
{
    int i;
    p_button_opr = opr;
    for(i = 0; i < opr->count; i++){
        device_create(button_class, NULL, MKDEV(major, i), NULL, "PB_button@%d",i);
    }
}

void button_operation_unregister(void)
{
    int i;
    for(i = 0; i < p_button_opr->count; i++){
        device_destroy(button_class, MKDEV(major, i));
    }
}

EXPORT_SYMBOL(button_operation_register);
EXPORT_SYMBOL(button_operation_unregister);

static ssize_t button_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    int minor = iminor(file_inode(file));
    char level;
    int err;
    level = p_button_opr->read(minor);  //函数
    err = copy_to_user(buf, &level, 1);
    return 1;
}

static int button_open(struct inode *node, struct file *flie)
{
    int minor = iminor(node);
    p_button_opr->init(minor);
    return 0;
}

int button_init(void)
{
    major = register_chrdev(0, "pb_button", &button_drv);
    button_class = class_create(THIS_MODULE, "pb_button");
    return 0;
}

void button_exit(void)
{
    class_destroy(button_class);   
    unregister_chrdev(major, "pb_button");
}

module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");

